home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWDebug / Sources / FWDbgStr.cpp next >
Encoding:
Text File  |  1994-04-21  |  10.2 KB  |  345 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWDbgStr.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifdef FW_DEBUG
  13.  
  14. #include <stddef.h>
  15.  
  16. #ifndef FWDBGSTR_H
  17. #include "FWDbgStr.h"
  18. #endif
  19.  
  20. #ifndef FWMATH_H
  21. #include "FWMath.h"
  22. #endif
  23.  
  24. #ifndef FWPRIMEM_H
  25. #include "FWPriMem.h"
  26. #endif
  27.  
  28. #ifndef FWPRISTR_H
  29. #include "FWPriStr.h"
  30. #endif
  31.  
  32. #ifndef FWPRIDEB_H
  33. #include "FWPriDeb.h"
  34. #endif
  35.  
  36. #if defined(FW_BUILD_MAC) && !defined(__FILES__)
  37. #include <Files.h>
  38. #endif
  39.  
  40. #if defined(FW_BUILD_MAC) && !defined(__ERRORS__)
  41. #include <Errors.h>
  42. #endif
  43.  
  44. #if defined(FW_BUILD_MAC) && !defined(__STRINGS__)
  45. #include <Strings.h>
  46. #endif
  47.  
  48. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  49. #include <Script.h>
  50. #endif
  51.  
  52. //========================================================================================
  53. //    CLASS FW_CDebugStream
  54. //========================================================================================
  55.  
  56. //----------------------------------------------------------------------------------------
  57. // FW_CDebugStream::EndLine
  58. //----------------------------------------------------------------------------------------
  59.  
  60. FW_CDebugStream &EndLine(FW_CDebugStream &stream)
  61. {
  62. #ifdef FW_BUILD_MAC
  63.     return stream.Write((void *) "\n", sizeof(char));
  64. #endif
  65. #ifdef FW_BUILD_WIN
  66.     return stream.Write((void *) "\n\r", sizeof(char) * 2);
  67. #endif
  68. }
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // FW_CDebugStream::FW_CDebugStream
  72. //----------------------------------------------------------------------------------------
  73.  
  74. FW_CDebugStream::FW_CDebugStream()
  75. {
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // FW_CDebugStream::~FW_CDebugStream
  80. //----------------------------------------------------------------------------------------
  81.  
  82. FW_CDebugStream::~FW_CDebugStream()
  83. {
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // FW_CDebugStream::WriteChunk
  88. //----------------------------------------------------------------------------------------
  89.  
  90. FW_CDebugStream &FW_CDebugStream::WriteChunk(const void *data, size_t size)
  91. {
  92.     Write(data, size);
  93.     return Write((void *) " ", sizeof(char));
  94. }
  95.  
  96. //----------------------------------------------------------------------------------------
  97. // FW_CDebugStream::WriteBase10Number(long n)
  98. //----------------------------------------------------------------------------------------
  99.  
  100. FW_CDebugStream & FW_CDebugStream::WriteBase10Number(long n)
  101. {
  102.     char buf[20];
  103.     char *s = &buf[19];
  104.     *--s = 0;
  105.     
  106.     for (int i = 0; n; n /= 10, i++)
  107.         *--s = n % 10 + '0';
  108.     
  109.     return WriteChunk((void *) s, FW_PrimitiveStringLength(s));
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // FW_CDebugStream::WriteBase16Number(long n)
  114. //----------------------------------------------------------------------------------------
  115.  
  116. FW_CDebugStream & FW_CDebugStream::WriteBase16Number(long n)
  117. {
  118.     char buf[11] = "0X00000000";
  119.     char *digits = "0123456789ABCDEF";
  120.     char *s = &buf[12];
  121.     
  122.     for (int i = 0; n; n /= 16, i++)
  123.         *--s = digits[n % 16];
  124.  
  125.     return WriteChunk((void *) s, 11);
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // FW_CDebugStream::operator<<(const signed char *string)
  130. //----------------------------------------------------------------------------------------
  131.  
  132. FW_CDebugStream & FW_CDebugStream::operator<<(const signed char *string)
  133. {
  134.     return WriteChunk((void *) string, FW_PrimitiveStringLength((const char *) string));
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // FW_CDebugStream::operator<<(signed char c)
  139. //----------------------------------------------------------------------------------------
  140.  
  141. FW_CDebugStream & FW_CDebugStream::operator<<(signed char c)
  142. {
  143.     return WriteChunk((void *) &c, sizeof(char));
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // FW_CDebugStream::operator<<(long n)
  148. //----------------------------------------------------------------------------------------
  149.  
  150. FW_CDebugStream & FW_CDebugStream::operator<<(long n)
  151. {
  152.     if (n < 0)
  153.         WriteChunk((void *) "-", sizeof(char));
  154.     return WriteBase10Number(n);
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------
  158. // FW_CDebugStream::operator<<(unsigned long n)
  159. //----------------------------------------------------------------------------------------
  160.  
  161. FW_CDebugStream & FW_CDebugStream::operator<<(unsigned long n)
  162. {
  163.     return WriteBase10Number(n);
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // FW_CDebugStream::operator<<(void *p)
  168. //----------------------------------------------------------------------------------------
  169.  
  170. FW_CDebugStream & FW_CDebugStream::operator<<(void *p)
  171. {
  172.     return WriteBase16Number((long) p);
  173. }
  174.  
  175.  
  176. //========================================================================================
  177. //    CLASS FW_CBufferDebugStream
  178. //========================================================================================
  179.  
  180. //----------------------------------------------------------------------------------------
  181. // FW_CBufferDebugStream::FW_CBufferDebugStream
  182. //----------------------------------------------------------------------------------------
  183.  
  184. FW_CBufferDebugStream::FW_CBufferDebugStream(void * buffer, size_t bufferSize) :
  185.     fBuffer(buffer),
  186.     fBufferSize(bufferSize),
  187.     fPosition(0)
  188. {
  189. }
  190.     
  191. //----------------------------------------------------------------------------------------
  192. // FW_CBufferDebugStream::FW_CBufferDebugStream
  193. //----------------------------------------------------------------------------------------
  194.  
  195. FW_CDebugStream &FW_CBufferDebugStream::Write(const void *data, size_t size)
  196. {
  197.     if (fPosition >= fBufferSize)
  198.         return *this;
  199.     
  200.     size_t writeAmmount = FW_Minimum(size, (size_t)(fBufferSize - fPosition));
  201.     
  202.     FW_PrimitiveCopyMemory(data, (void*)fBuffer, writeAmmount);
  203.     
  204.     return *this;
  205. }
  206.  
  207. //========================================================================================
  208. //    CLASS FW_CNullDebugStream
  209. //========================================================================================
  210.  
  211. //----------------------------------------------------------------------------------------
  212. // FW_CNullDebugStream::FW_CNullDebugStream
  213. //----------------------------------------------------------------------------------------
  214.  
  215. FW_CNullDebugStream::FW_CNullDebugStream()
  216. {
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // FW_CNullDebugStream::~FW_CNullDebugStream
  221. //----------------------------------------------------------------------------------------
  222.  
  223. FW_CNullDebugStream::~FW_CNullDebugStream()
  224. {
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // FW_CNullDebugStream::Write
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_CDebugStream &FW_CNullDebugStream::Write(const void *data, size_t size)
  232. {
  233.     return *this;
  234. }
  235.  
  236.  
  237. //========================================================================================
  238. // CLASS FW_CFileDebugStream
  239. //========================================================================================
  240.  
  241. //----------------------------------------------------------------------------------------
  242. // FW_CFileDebugStream::FW_CFileDebugStream
  243. //----------------------------------------------------------------------------------------
  244.  
  245. FW_CFileDebugStream::FW_CFileDebugStream(char *fileName)
  246. {
  247. #ifdef FW_BUILD_MAC
  248.     FSSpec spec;
  249.     
  250.     OSErr err = FSMakeFSSpec(0, 0, c2pstr(fileName), &spec);
  251.     p2cstr((unsigned char *) fileName); // c2pstr modifies the string so lets put it back the way it was
  252.     if (err == fnfErr)
  253.     {
  254.         err = FSpCreate(&spec, 'MPS ', 'TEXT', langEnglish);
  255.         if (err)
  256.         {
  257.             FW_PRIV_DEBUGGER_STRING(
  258.                 "FW_CFileDebugStream::FW_CFileDebugStream: can't create file.");
  259.         }
  260.     }
  261.     else if (err)
  262.     {
  263.         FW_PRIV_DEBUGGER_STRING(
  264.             "FW_CFileDebugStream::FW_CFileDebugStream: invalid path.");
  265.     }
  266.     
  267.     err = FSpOpenDF(&spec, fsWrPerm, &fMacFileNumber);
  268.     if (err)
  269.         FW_PRIV_DEBUGGER_STRING(
  270.             "FW_CFileDebugStream::FW_CFileDebugStream: can't open file.");
  271.     
  272.     err = SetEOF(fMacFileNumber, 0);
  273.     if (err)
  274.         FW_PRIV_DEBUGGER_STRING(
  275.             "FW_CFileDebugStream::FW_CFileDebugStream: can't reset EOF.");
  276. #endif
  277. #ifdef FW_BUILD_WIN
  278.     fWinFileHandle = _lcreat(fileName, 0);
  279.         // Create, or open and truncate a file for read and write
  280.     
  281.     if (fWinFileHandle == HFILE_ERROR)
  282.         FW_PRIV_DEBUGGER_STRING(
  283.             "FW_CFileDebugStream::FW_CFileDebugStream: can't create/open file.");
  284. #endif
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. // FW_CFileDebugStream::~FW_CFileDebugStream
  289. //----------------------------------------------------------------------------------------
  290.  
  291. FW_CFileDebugStream::~FW_CFileDebugStream()
  292. {
  293. #ifdef FW_BUILD_MAC
  294.     OSErr err = FSClose(fMacFileNumber);
  295.     
  296.     if (err)
  297.         FW_PRIV_DEBUGGER_STRING(
  298.             "FW_CFileDebugStream::~FW_CFileDebugStream: can't close.");
  299. #endif
  300. #ifdef FW_BUILD_WIN
  301.     HFILE result = _lclose(fWinFileHandle);
  302.     
  303.     if (result == HFILE_ERROR)
  304.         FW_PRIV_DEBUGGER_STRING(
  305.             "FW_CFileDebugStream::~FW_CFileDebugStream: can't close.");
  306. #endif
  307. }
  308.  
  309. //----------------------------------------------------------------------------------------
  310. // FW_CFileDebugStream::Write
  311. //----------------------------------------------------------------------------------------
  312.  
  313. FW_CDebugStream & FW_CFileDebugStream::Write(const void *data,
  314.                                             size_t size)
  315. {
  316. #ifdef FW_BUILD_MAC
  317.     long count = size;
  318.     
  319.     OSErr err = FSWrite(fMacFileNumber, &count, (Ptr) data);
  320.     if (err)
  321.     {
  322.         FSClose(fMacFileNumber);
  323.         // Go ahead and try closing the file
  324.         
  325.         FW_PRIV_DEBUGGER_STRING(
  326.             "FW_CFileDebugStream::FW_CFileDebugStreamWrite: can't write.");
  327.     }
  328. #endif
  329. #ifdef FW_BUILD_WIN
  330.     long written = _hwrite(fWinFileHandle, data, size);
  331.  
  332.     if (written == -1L)
  333.     {
  334.         _lclose(fWinFileHandle);
  335.             // Go ahead and try closing the file
  336.         
  337.         FW_PRIV_DEBUGGER_STRING(
  338.             "FW_CFileDebugStream::FW_CFileDebugStreamWrite: can't write.");
  339.     }
  340. #endif
  341.     
  342.     return *this;
  343. }
  344.  
  345. #endif // FW_DEBUG